import Vue from 'vue';
import VueRouter from 'vue-router';
import nprogress from 'nprogress';
import { pendingRequest } from '@/utils/request';

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    name: 'Index',
    component: () => import('@/views/index.vue'),
  },
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  nprogress.start();
  next();
  // 取消上个页面未完成的请求
  pendingRequest.forEach((item) => item.routeChangeCancel && item.cancel());
  nprogress.done();
});

export default router;
